﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Tour.Linq.TourDBML;

public partial class AdminManager_Admin_AreaManager_Add : AdminBasePage
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDDL();
            DisplayInfo();
        }
    }

    private void DisplayInfo()
    {
        if (!IsCreate)
        {
            TourArea model = TourContext.TourArea.SingleOrDefault(p => p.AreaID == AreaID);
            if (model != null)
            {
                txtTypeName.Text = model.AreaName;
                ddlParentID.SelectedValue = model.ParentID.ToString();
            }
        }
    }

    private void BindDDL()
    {
        ddlParentID.DataTextField = "AreaName";
        ddlParentID.DataValueField = "AreaID";
        ddlParentID.DataSource = TourStaticData.TreeTourArea;
        ddlParentID.DataBind();
        ddlParentID.Items.Add(new ListItem("主地区", "0"));
        ddlParentID.SelectedValue = ParentID.ToString();
        
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        TourArea model;
        if (IsCreate)
        {
            model = new TourArea();
            _Message = "添加新地区成功";
        }
        else
        {
            model = TourContext.TourArea.SingleOrDefault(p => p.AreaID == AreaID);
            _Message = "修改地区成功";
        }
        model.AreaName = txtTypeName.Text;
        model.ParentID = ddlParentID.SelectedValue.ToInt();

        if (IsCreate)
        {
            TourContext.TourArea.InsertOnSubmit(model);
        }
        TourStaticData.Clean();
        TourContext.SubmitChanges();
    }

    protected int ParentID
    {
        get
        {
            return Request["ParentID"].ToInt();
        }
    }

    protected int AreaID
    {
        get
        {
            return Request["AreaID"].ToInt();
        }
    }

    protected bool IsCreate
    {
        get
        {
            return AreaID == 0;
        }
    }
}
